package cn.com.huzo.repository.master.impl;

import cn.com.huzo.model.entities.master.SizeEntity;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import cn.com.huzo.repository.master.SizeRepository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;

/**
 * 尺码数据访问对象。
 *
 * @author 吴旭
 */
@Repository("SizeRepository")
public class SizeRepositoryImpl extends GenericRepositoryImpl<SizeEntity, Long>
        implements SizeRepository {

    private final String namespace = "cn.com.huzo.repository.master.SizeRepository";

    /**
     * 构造函数,设置the entity to SizeRepositoryImpl.class.
     */
    public SizeRepositoryImpl() {
        super(SizeEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SizeEntity fetchByKey(String rootNodeName, Long key) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("RootNodeName", rootNodeName);
        criterion.put("SizeKey", key);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchByKey"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchForCount(Map<String, Object> criterion) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForCount"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<SizeEntity> fetchForPage(Map<String, Object> criterion) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchForPage"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<SizeEntity> fetchAllByCatalogueKey(Long catalogueKey) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchAllByCatalogue"), catalogueKey);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Long insert(SizeEntity instance) {
        getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insert"), instance);

        // 返回出入后的记录ID
        return instance.getSizeId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer update(SizeEntity instance) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), instance);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(Long key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer batchChangeCatalogue(List<SizeEntity> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "batchChangeCatalogue"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String fetchNewOrderNo(Long catalogueId) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchNewOrderNo"), catalogueId);
    }
}
